<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>$!app_name</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<!-- 框架 -->
    <link href="${base}/include/css/msg.css" rel="stylesheet" type="text/css"/>

    <link href="${base}/include/css/default.css" rel="stylesheet" type="text/css"/>
	<script type='text/javascript'>
		var CONTEXTPATH = '${base}';
	</script>
    <script src="${base}/include/js/main.js"></script>
    <script src="${base}/include/js/string.js"></script>
	<!-- 分页  注意顺序 -->
	<link href="${base}/include/css/themes/gray/easyui.css" rel="stylesheet" type="text/css"/> 	
	<script src="${base}/include/js/jquery.easyui.min.js"></script>	
    <!-- 树 -->
    <script type="text/javascript" src="${base}/include/controls/ztree/jquery.ztree.core-3.5.min.js"></script> 
	<link rel="stylesheet" href="${base}/include/controls/ztree/zTreeStyle/zTreeStyle.css" type="text/css">

       <SCRIPT type="text/javascript">
       <!--
		var setting = {
			async: {
                   enable: true,
                   url:"${base}/private/sys/role/tree",
                   autoParam:["id", "name=n", "level=lv"]
            },
			view: {
				selectedMulti: false ,
                nameIsHTML: true
			},
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};
		var myTree;
		function initTree(){
        	myTree =jQuery.fn.zTree.init(jQuery("#treeDemo"), setting);
		} 
		//调用添加页面
		function add()
		{
			var d = new Dialog("Add");
			d.Width = 650;
			d.Height = 440;
			d.Title = "新建角色";
			d.URL = "${base}/private/sys/role/toadd";
			d.OKEvent = function(){
				addSave();
			};
			d.onLoad = function() {
				$DW.$("name").focus();
			}
			d.show();
		}
	   
		//执行添加操作
		function addSave(){     
			if($DW.Verify.hasError()){
			  return;
			 }    	
			 if($DW.dosave()){
			 jQuery.ajax({
					async:false,
					type: 'POST',
					url : "${base}/private/sys/role/add",
					data : $DW.$("#form1").serialize(),
					success : function (res) { 
						if(res>0){
							$D.close();
							Dialog.alert("添加成功！");
							initTree();
							list(res);
						}else{
							Dialog.alert("添加失败！");
						}
						 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误?!");
					}
				});
			 }
		}
		//调用更新页面
		function update(){  
			var id=Page.getValue("id");
			var d = new Dialog("Update");
			d.Width = 380;
			d.Height = 160;
			d.Title = "修改角色";
			d.URL = "${base}/private/sys/role/toupdate?id="+id;
			d.OKEvent = function() {           
					updateSave(id);
			}
			d.onLoad = function() {
				$DW.$("name").focus();
			}
			d.show();
		}
		
		//更新操作
		function updateSave(id){     
			if($DW.Verify.hasError()){
			  return;
			 }   
			
			if($DW.doSave()){
			 jQuery.ajax({
					type: 'POST',
					url : "${base}/private/sys/role/update" ,
					data : $DW.$("#form1").serialize(),
					success : function (res) { 
						if(res=="true"){
							$D.close();
							Dialog.alert("修改成功！");
							initTree();
							list(id);
						}else{
							Dialog.alert("修改失败！");
						}
						 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误!");
					}
				});  
			}
		}
		//删除角色
		function del(){   	
			var id=Page.getValue("id");
			Dialog.confirm("确定删除？", function(){
				jQuery.ajax({
					type: 'POST',
					url : "${base}/private/sys/role/delete?id="+id ,    		
					success : function (res) { 
						if(res!=""){    				
							Dialog.alert("删除成功！");
							initTree();
							list("$!roleid");
							 
						}else{
							Dialog.alert("删除失败！");
						}			 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误!");
					}
				});
			}, "", 250, 150);    
		}
		//转到权限分配页面
		function tomenu()
		{
			var id=Page.getValue("id");
			var d = new Dialog("UpdateMenu");
			d.Width = 560;
			d.Height = 400;
			d.Title = "权限分配";
			d.URL = "${base}/private/sys/role/tomenu?roleid="+id;
			d.OKEvent = function() {
				tomenuSave();
			};
			d.show();
		}
		function tomenuSave(){     
			if($DW.Verify.hasError()){
			  return;
			 }     	
			if($DW.doSave()){
			 jQuery.ajax({
					type: 'POST',
					url : "${base}/private/sys/role/menu" ,
					data : $DW.$("#form1").serialize(),
					success : function (res) { 
						if(res=="true"){
							$D.close();
							Dialog.alert("分配成功！");
						}else{
							Dialog.alert("分配失败！");
						}
						 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误!");
					}
				});  
				}
		  }
       //转到CMS权限分配页面
       function tocmsmenu()
       {
           var id=Page.getValue("id");
           var d = new Dialog("UpdateCmsMenu");
           d.Width = 560;
           d.Height = 400;
           d.Title = "栏目权限分配";
           d.URL = "${base}/private/sys/role/tochannel?roleid="+id;
           d.OKEvent = function() {
               channelSave();
           }
           //d.ShowMessageRow = true;
           //d.MessageTitle = "分配权限";
           //d.Message = "通过选中菜单赋予权限,并可以选择对菜单的操作！";
           d.show();
       }
       function channelSave(){
           if($DW.Verify.hasError()){
               return;
           }
           if($DW.doSave()){
               jQuery.ajax({
                   type: 'POST',
                   url : "${base}/private/sys/role/channel" ,
                   data : $DW.$("#form1").serialize(),
                   success : function (res) {
                       if(res=="true"){
                           $D.close();
                           Dialog.alert("分配成功！");
                       }else{
                           Dialog.alert("分配失败！");
                       }

                       return false;
                   },
                   fail : function(res) {
                       Dialog.alert("系统错误!");
                   }
               });
           }
       }
		   //调用添加用户到角色页面
		  function adduser()
		 {
			var id=Page.getValue("id");
			var d = new Dialog("AddUser");
			d.Width = 850;
			d.Height = 430;
			d.Title = "用户列表";
			d.URL = "${base}/private/sys/role/toaddroleuser?roleid="+id;
			d.OKEvent = function() {           
					todAduser();                       
			};
			d.show();
		  }

		  function todAduser(){     
			if($DW.Verify.hasError()){
			  return;
			 }    
			var id=Page.getValue("id");
			if($DW.doSave()){
			 jQuery.ajax({
					type: 'POST',
					url : "${base}/private/sys/role/addroleuser?roleid="+id ,
					data : $DW.$("#form1").serialize(),
					success : function (res) { 
						if(res=="true"){
							$D.close();
							Dialog.alert("用户分配成功！");
							list(id);
						}else{
							Dialog.alert("用户分配失败！");
						}
						 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误!");
					}
				});  
			}
		}
    
		//提交删除增加列表判断
		function deluser()
		{
			var arr = $('#dg1').datagrid('getSelections');
			var id=Page.getValue("id");
			var ids = [];
			if (arr == null || arr.length == 0) {
				Dialog.alert("请选择角色中用户！");
				return;
			}
			for (var i = 0; i < arr.length; i++) {
				ids.push(arr[i].id);
				if(arr[i].id=="10000"){
					if(id=="1"){
						Dialog.alert("超级管理员不可从公共角色移除！");
						return;
					}
					if(id=="2"){
						Dialog.alert("超级管理员不可从系统管理角色移除！");
						return;
					}
				}
			}
			Dialog.confirm("确认要移除？", function() {
				jQuery.ajax({
					type: 'POST',
					url : "${base}/private/sys/role/delroleuser?roleid="+id+"&checkids="+ids ,    		
					success : function (res) { 
						if(res=="true"){    				
							Dialog.alert("移除成功！");
							list(id);
						}else{
							Dialog.alert("移除失败！");
						}			 
						return false;
					},
					fail : function(res) {
						Dialog.alert("系统错误!");
					}
				});
			});

		}
		   function getWidth(percent){
        	   return document.body.clientWidth*percent;
		   }
           function loadGrid(roleid){
        	   $("#dg1").datagrid({  
        	        width:"auto",
        	   		height:"auto",
        	        nowrap: false,  
        	        striped: true,  
        	        border: true,  
        	        collapsible:false,//是否可折叠的  
        	        fit: true,//自动大小  
        	        url:"${base}/private/sys/role/list?roleid="+roleid,  
        	        //sortName: 'code',  
        	        //sortOrder: 'desc',  
        	        remoteSort:false,   
        	        idField:"userid",
        	        singleSelect:false,//是否单选  
        	        pagination:true,//分页控件  
        	        rownumbers:true,//行号  
        	        fitColumns: true,//行自适应
        	        pageList:[5,10,15,20,30,40,50],
        	        loadMsg:'数据加载中...',
        	        columns:[[
						{title : '用户名',field : 'loginname',width:getWidth(0.08),align : 'left'},
						{title : '姓名',field : 'realname',width:getWidth(0.08),align : 'left'},
						{title : '帐户状态',field : 'state',align : 'left',
								formatter:function(value,rec,index){
	            					if(rec.state==1)
	            					{
										return '<font color="red">禁用</font>';
	            					}
	            					else
	            					{
	            						return '正常';
	            					}
       					}},
						{title : '职务',field : 'pozition',width:getWidth(0.10),align : 'left'},
						{title : '电子邮件',field : 'email',width:getWidth(0.10),align : 'left'},
						{title : '固定电话',field : 'telephone',width:getWidth(0.10),align : 'left'},
						{title : '手机号码',field : 'mobile',width:getWidth(0.10),align : 'left'}   
        	        ]],
        	        frozenColumns:[[  
        	            {field:"userid",checkbox:true}
        	        ]]
        	    });  
        	    //设置分页控件  
        	    var p = $("#dg1").datagrid("getPager");  
        	    $(p).pagination({  
        	        beforePageText: "第",//页数文本框前显示的汉字  
        	        afterPageText: "页    共 {pages} 页",  
        	        displayMsg: "当前显示 {from} - {to} 条记录   共 {total} 条记录",  
        	        onBeforeRefresh:function(){ 
        	            //$(this).pagination('loading'); 
        	             
        	            //$(this).pagination('loaded'); 
        	        } 
        	    });  
           }
		function list(roleid){
        	   jQuery.ajax({
          		 	type: 'POST',
          			url : "${base}/private/sys/role/getrole" ,
          			data : {"roleid":roleid},
          			success : function (res) { 
          				Page.bindData("dg0",res,""); 
						if(roleid==1||roleid==2||roleid==$!roleid){
							$Z("BtnDel").disable();
						}else{
							$Z("BtnDel").enable();
						}
          				loadGrid(roleid);
 
          			},
          			fail : function(res) {
          				Dialog.alert("系统错误?!");
          			}
          		});
        	    
           }
		
		Page.onLoad(function() {
        	   initTree();
			   list("$!roleid");
        	   Page.initBtn();
        	   $!initBtn
        	   
        }); 
		$(window).resize(function () {
			clearTimeout(timer);
			timer=setTimeout(function(){
			  $('#dg1').datagrid('resize');
			  $('div.panel-body').css('width','auto'); //////
			 },100);
			});
           //-->
       </SCRIPT>

<head>
<body leftmargin="0" topmargin="0" style="overflow-x: hidden; overflow-y: auto">
<table width="100%" border="0" cellspacing="6" cellpadding="0"
       style="border-collapse: separate; border-spacing: 6px;">
    <tr valign="top">
        <td width="180">
            <table width="210" height="100%" border="0" cellspacing="0" cellpadding="6"
                   class="blockTable">
                <tr>
                    <td style="padding:6px;" class="blockTd">
                        <div ztype='_Tree' style='-moz-user-select:none;height:460px;width:200px;' id='tree1' class='treeItemN'>

		         <ul id="treeDemo" class="ztree"></ul>
                    </div>

                    </td>
                </tr>
            </table>
            

        </td>
        <td width="100%" valign="top">
            <form name='form1' id="form1" method='post'>
    <input type="hidden" name="id" id="id" value="$!roleid">
    <input type="hidden" name="checkids" id="checkids" value="">
    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="blockTable"
           style="border-collapse: separate; border-spacing: 0;">
        <tr valign="top">
            <td class="blockTd">
                <table width="100%" border="0" cellspacing="0" cellpadding="6">

                    <tr>
                                    <td valign="middle"><img src="${base}/images/icons/icon021a8.gif" width="20" height="20"/>&nbsp;角色信息
                                    </td>
                                </tr>
                    <tr>
                        <td style="padding:0 8px 4px;"><a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn'
                                                          hidefocus='true' tabindex='-1' onselectstart='return false'
                                                          id='BtnAdd' onClick="add();"><img
                                src="${base}/images/icons/icon022a2.gif" width="20"
                                height="20"/><b>新建&nbsp;</b></a>
                            <a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn' hidefocus='true'
                               tabindex='-1' onselectstart='return false' id='BtnUpdate'
                               onClick="update();return false;"><img
                                    src="${base}/images/icons/icon022a4.gif" width="20"
                                    height="20"/><b>编辑&nbsp;</b></a>
                            <a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn' hidefocus='true'
                               tabindex='-1' onselectstart='return false' id='BtnDel'
                               onClick="del();"><img
                                    src="${base}/images/icons/icon022a3.gif"
                                    width="20" height="20"/><b>删除&nbsp;</b></a>
                           <a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn' hidefocus='true'
                               tabindex='-1' onselectstart='return false' id='BtnMenu'
                               onClick="tomenu();return false;"><img
                                    src="${base}/images/icons/icon_key.gif" width="20"
                                    height="20"/><b>分配菜单权限&nbsp;</b></a>
                            <!--<a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn' hidefocus='true'-->
                               <!--tabindex='-1' onselectstart='return false' id='BtnMenu'-->
                               <!--onClick="tocmsmenu();return false;"><img-->
                                    <!--src="${base}/images/icons/icon_key.gif" width="20"-->
                                    <!--height="20"/><b>分配栏目权限&nbsp;</b></a>-->
                                    
                        </td>

                    </tr>
                    <tr>
                        <td style="padding:0px 5px;">

                            <table width="100%" id="dg0" cellpadding="2" cellspacing="0" class="dataTable"
                                   afterdrag="sortMenu" sortstring="" page="false" size="0" multiselect="true"
                                   autofill="true" scroll="false" lazy="false" cachesize="0">
                                <thead>
                                <tr ztype="head" class="dataTableHead">
                                    <td width="18%"><b>角色名称</b></td>
                                    <td><strong>角色描述</strong></td>
                                </tr>
                                </thead>
								<tr >
                                    <td width="18%"><div id="name"></div></td>
                                    <td ><div id="descript"></div></td>
                                </tr>
                                <tbody>                              
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="6">
                    <tr>
                        <td style="padding:0 8px 4px;"><a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn'
                                                          hidefocus='true' tabindex='-1' onselectstart='return false'
                                                          id='BtnAddRole' onClick="adduser();"><img
                                src="${base}/images/icons/icon021a2.gif" width="20"
                                height="20"/><b>添加用户到角色&nbsp;</b></a>
                            <a href='javascript:void(0);' ztype='zPushBtn' class='zPushBtn' hidefocus='true'
                               tabindex='-1' onselectstart='return false' id='BtnDelRole'
                               onClick="deluser();"><img
                                    src="${base}/images/icons/icon021a3.gif" width="20"
                                    height="20"/><b>从角色中移除用户&nbsp;</b></a></td>
                    </tr>
                    <tr>
                        <td style="padding:0px 5px;" height="320">

                            <table width="100%" height="100%" id="dg1" size="10" cellpadding="2" cellspacing="0">
												<tr><td></td></tr>
							</table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>

        </td>
    </tr>
</table>
</body>
</html>
